<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>转换二维数组</title>
</head>
<body>
  <script>
    function fn(nums) {
      const res = []
      for (const num of nums) {
        let l = 0
        while (res[l] && res[l].includes(num)) l++
        res[l] = res[l] || []
        res[l].push(num)
      }
      return res
    }
    console.log(fn([1, 3, 4, 1, 2, 3, 1]))
  </script>
</body>
</html>